package com.lcf.app.dingtalk;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiSmartbotMsgPushRequest;
import com.dingtalk.api.request.OapiUserListidRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiSmartbotMsgPushResponse;
import com.dingtalk.api.response.OapiUserListidResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.taobao.api.ApiException;

public class Demo {

    public static void main(String[] args) {
        String appkey = "*******";
        String appsecret = "*******";
        try {
            // 获取access_token
            String body = getAccessToken(appkey, appsecret);
            // {"errcode":0,"access_token":"079a6a6ec88938f79a2ba9f7aa7d0dc0","errmsg":"ok","expires_in":7200}
            System.out.println(body);
            JSONObject jsonObject = JSONObject.parseObject(body);
            int errcode = jsonObject.getIntValue("errcode");
            if (errcode == 0) {
                String accessToken = jsonObject.getString("access_token");
                accessToken = "079a6a6ec88938f79a2ba9f7aa7d0dc0";

                String departIdList = getDepartIdList(accessToken);
                System.out.println(departIdList);


                String userIdList = getUserIdList(accessToken);
                System.out.println(userIdList);

                //调用本接口通过服务助手机器人给企业员工发送消息。
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartbot/msg/push");
                OapiSmartbotMsgPushRequest req = new OapiSmartbotMsgPushRequest();
                OapiSmartbotMsgPushRequest.Text text = new OapiSmartbotMsgPushRequest.Text();
                text.setContent("请提交日报。");
                OapiSmartbotMsgPushRequest.Msg msg = new OapiSmartbotMsgPushRequest.Msg();
                msg.setText(text);
                msg.setMsgtype("text");
                req.setUserIdList("user456");
                req.setMsg(msg);
                OapiSmartbotMsgPushResponse rsp = client.execute(req, accessToken);
                System.out.println(rsp.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    // 获取部门列表
    private static String getDepartIdList(String accessToken) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
        OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
        //req.setDeptId(1L);
        req.setLanguage("zh_CN");
        OapiV2DepartmentListsubResponse rsp = client.execute(req, accessToken);
        return rsp.getBody();
    }

    private static String getUserIdList(String accessToken) throws ApiException {
        // 获取用户userId 列表
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
        OapiUserListidRequest req = new OapiUserListidRequest();
        req.setDeptId(100L);
        OapiUserListidResponse rsp = client.execute(req, accessToken);
        return rsp.getBody();
    }

    private static String getAccessToken(String appkey, String appsecret) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appkey);
        request.setAppsecret(appsecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        return response.getBody();
    }

}
